<template>
  <a-upload
    :accept="accept"
    :disabled="disabled || !!uploadState.stateList.length"
    :before-upload="beforeUpload"
    :custom-request="customRequest"
    :multiple="multiple"
    :show-upload-list="false"
  >
    <div class="image-box">
      <div
        v-for="(item, index) in state.fileList"
        :key="item.fileId"
        class="file-item"
        :style="sizeStyle"
      >
        <img
          v-show="item.loaded"
          @load="item.loaded = true"
          :src="item.url"
          :alt="item.originFileName"
          :style="sizeStyle"
        />
        <div v-show="!item.loaded" :style="sizeStyle" class="preview">图</div>
        <div class="mask" :style="sizeStyle">
          <div class="operation">
            <a-tooltip :title="t('编辑')" v-if="!disabled">
              <SelectOutlined class="operation-item" />
            </a-tooltip>
            <a-tooltip :title="t('预览')">
              <EyeOutlined
                class="operation-item"
                @click.stop="handlePreview(index)"
              />
            </a-tooltip>
          </div>
          <div class="operation">
            <a-tooltip :title="t('下载')">
              <CloudDownloadOutlined
                class="operation-item"
                @click.stop="download(item)"
              />
            </a-tooltip>
            <a-tooltip :title="t('删除')" v-if="!disabled">
              <DeleteOutlined
                class="operation-item"
                @click.stop="deleteFile(item.fileId)"
              />
            </a-tooltip>
          </div>
        </div>
      </div>
      <div
        v-for="item in uploadState.stateList"
        class="file-item"
        :key="item.fileMd5"
      >
        <a-progress
          :percent="item.schedule"
          :size="Math.min(width, height)"
          type="circle"
          status="active"
          :format="customFormat"
        />
      </div>
      <div
        v-show="
          (multiple &&
            state.fileList.length + uploadState.stateList.length <
              maxFileNums) ||
          (!multiple && !state.fileList.length && !uploadState.stateList.length)
        "
        class="upload-btn file-item"
        :style="sizeStyle"
      >
        <div style="text-align: center">
          <div><PlusOutlined /></div>
          {{ t("上传") }}
        </div>
      </div>
    </div>
  </a-upload>
</template>
<script lang="ts" setup>
import { useI18n } from "vue-i18n";
import type { Props } from "../typing";
import { computed } from "vue";

const props = defineProps<Props>();
const { t } = useI18n();
const sizeStyle = computed(() => ({
  width: props.width + "px",
  height: props.height + "px",
}));
// 自定义进度文本
const customFormat = (percent: number) => {
  if (percent == 100) {
    return t("请稍后...");
  } else {
    return percent + "%";
  }
};
</script>
<style lang="less" scoped>
.image-box {
  display: flex;
  flex-wrap: wrap;
  .upload-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    border: 2px dashed #cccccc;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
    color: #666666;
    &:hover {
      color: @primary-color;
      border-color: @primary-color;
    }
  }
  .file-item {
    position: relative;
    border-radius: 8px;
    margin: 4px;
    overflow: hidden;
    img {
      object-fit: cover;
    }
    .preview {
      background-color: rgba(0, 0, 0, 0.5);
      color: #ffffff;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .mask {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: calc(100% - 4px);
      opacity: 0;
      transition: all 0.3s;
      background-color: rgba(0, 0, 0, 0.3);
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: center;
      color: #ffffff;
      &:hover {
        opacity: 1;
      }
      .operation {
        width: 100%;
        display: flex;
        justify-content: space-around;
        &-item {
          font-size: 16px;
          cursor: pointer;
          transition: transform 0.3s;
          &:hover {
            transform: scale(1.2);
          }
        }
      }
    }
  }
}
</style>
